//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using Shp.Runtime.Contract;

namespace LitwareHR.Portfolio.Gateways
{
    static public class DatamodelGateway
    {
        static public Guid AddInstance(string entityDefName, EntityField[] fields)
        {
            using (SecureChannel  channel = new SecureChannel())
            {
                return channel.AddInstance(entityDefName, fields);
            }
        }

        static public StringPair[][] GetInstanceList(string tenantAlias, string entityDefName, Guid[] listToRetrieve)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetInstanceList(tenantAlias, entityDefName, listToRetrieve);
            }
        }

        static public StringPair[] GetViewDefFields(string tenantAlias, string viewDefName)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetViewDefFields(tenantAlias, viewDefName);
            }
        }

        static public EntityField[] GetEntityDefFields(string tenantAlias, string entityDefName)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetEntityDefFields(tenantAlias, entityDefName);
            }
        }

        static public StringPair[] GetViewDefFieldsById(string tenantAlias, Guid viewDefId)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetViewDefFieldsById(tenantAlias, viewDefId);
            }
        }

        static public Dictionary<Guid, string> GetEntityDefList()
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetEntityDefList();
            }
        }

        static public Dictionary<Guid, string> GetViewDefList()
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetViewDefList();
            }
        }

        static public GuidStringPair[] GetTypeList()
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetTypeList();
            }
        }

        static public void UpdateViewDef(Guid viewDefId, StringPair[] fields)
        {
            using (SecureChannel channel = new SecureChannel())
            {
                channel.UpdateViewDef(viewDefId, fields);
            }
        }

        static public void UpdateEntityDef(Guid entityDefId, EntityField[] fields)
        {
            using (SecureChannel channel = new SecureChannel())
            {
                channel.UpdateEntityDef(entityDefId, fields);
            }
        }

        private class SecureChannel : ClientBase<IDatamodelSC>, IDatamodelSC
        {
            #region IDatamodelSC Members

            public void UpdateViewDef(Guid viewDefId, StringPair[] fields)
            {
                base.Channel.UpdateViewDef(viewDefId, fields);
            }

            public void UpdateEntityDef(Guid entityDefId, EntityField[] fields)
            {
                base.Channel.UpdateEntityDef(entityDefId, fields);
            }

            public Guid AddInstance(string entityDefName, EntityField[] fields)
            {
                return base.Channel.AddInstance(entityDefName, fields);
            }

            #endregion
        }

        private class UnSecureChannel : ClientBase<IDatamodelUC>, IDatamodelUC
        {
            #region IDatamodelUC Members

            public StringPair[][] GetInstanceList(string tenantAlias, string entityDefName, Guid[] listToRetrieve)
            {
                return base.Channel.GetInstanceList(tenantAlias, entityDefName, listToRetrieve);
            }

            public StringPair[] GetViewDefFields(string tenantAlias, string viewDefName)
            {
                return base.Channel.GetViewDefFields(tenantAlias, viewDefName);
            }

            public StringPair[] GetViewDefFieldsById(string tenantAlias, Guid viewDefId)
            {
                return base.Channel.GetViewDefFieldsById(tenantAlias, viewDefId);
            }

            public EntityField[] GetEntityDefFields(string tenantAlias, string entityDefName)
            {
                return base.Channel.GetEntityDefFields(tenantAlias, entityDefName);
            }

            public Dictionary<Guid, string> GetEntityDefList()
            {
                return base.Channel.GetEntityDefList();
            }

            public Dictionary<Guid, string> GetViewDefList()
            {
                return base.Channel.GetViewDefList();
            }

            public GuidStringPair[] GetTypeList()
            {
                return base.Channel.GetTypeList();
            }

            #endregion
        }
    }
}